home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / util / arc / CheckX.lha / CheckX / sources / CheckX.c next >
Encoding:
C/C++ Source or Header  |  2000-08-31  |  43.8 KB  |  1,501 lines

  1. #define NAME         "CheckX"
  2. #define REVISION     "79"
  3. #define DISTRIBUTION "(Freeware) "
  4. #define DATE         "27.07.2000"
  5.  
  6. /*
  7. This program scans for crunched, linked files and archived files. It
  8. decrunches them and saves the result files to another directory-tree.
  9. The scanning routines are recursiv and thus check really all stuff.
  10.  
  11. The program must be compiled and linked without startup-code. You can set
  12. the pure file protection bit and make it resident, because it is multi-
  13. reentrant (no global variables, except library bases).
  14.  
  15. Crypted archives are not supported yet (e.g. asking for password).
  16. */
  17.  
  18. /* Programmheader
  19.  
  20.     Name:        CheckX
  21.     Author:        SDI
  22.     Distribution:    Freeware
  23.     Description:    scans, decrunches and dearchives files
  24.     Compileropts:    -
  25.     Linkeropts:    -l xpkmaster xadmaster amiga -gsi
  26.  
  27.  1.0   14.12.96 : first Version
  28.  1.1   28.12.96 : moved PassRequest into xpkmaster.library
  29.  1.2   12.02.97 : now also decrunches Exe-Files
  30.  1.3   15.06.97 : added length output as test
  31.  1.4   21.11.97 : renamed from Decrunch, got really new program
  32.  1.5   22.11.97 : bug-fixes
  33.  1.6   29.11.97 : added unarchiving feature
  34.  1.7   30.11.97 : bug-fixes
  35.  1.8   06.12.97 : xpkmaster.library now only required with ASKPWD option
  36.  1.9   07.12.97 : Added archive copy for weird archive names, better error
  37.     codes
  38.  1.10  08.12.97 : fixed error codes a bit
  39.  1.11  11.12.97 : disabled DOS requests, added Zip-Archives, added TaskID
  40.     to temporary filenames
  41.  1.12  12.12.97 : added Arc, ZOO and LhASFX archives
  42.  1.13  13.12.97 : fixed Arc recognition
  43.  1.14  19.12.97 : deletes copied arc before scan
  44.  1.15  22.12.97 : crunched archives are unarchived correctly now
  45.  1.16  02.01.98 : opens dos.library itself, no startup-code required
  46.  1.17  23.01.98 : added disk crunchers
  47.  1.18  24.01.98 : some fixes
  48.  1.19  01.02.98 : little bug-fix in argument-option use
  49.  1.20  06.02.98 : better error output, added automount
  50.  1.21  08.02.98 : little bug-fix
  51.  1.22  10.02.98 : fixed archive copy conditions, added PRINTALL
  52.  1.23  12.02.98 : fixed help text, bug fixes with unlinking and FreeMem
  53.  1.24  13.02.98 : added unstripping
  54.  1.25  04.03.98 : added PRINTEXEC
  55.  1.26  13.03.98 : added high density DMS support
  56.  1.27  19.03.98 : added LOUD keyword
  57.  1.28  23.03.98 : RDx no longer depends on archive depth, but on dddepth
  58.  1.29  10.04.98 : bug fixes
  59.  1.30  26.04.98 : bug fixes
  60.  1.31  09.05.98 : now uses no longer adress 4 for SysBase
  61.  1.32  31.05.98 : better output
  62.  1.33  04.06.98 : added HEADER addition for address files
  63.  1.34  08.08.98 : bug fix with SAVE
  64.  1.35  24.09.98 : added xvs.library virus checks
  65.  1.36  18.10.98 : xvs is opened global and only once
  66.  1.37  30.10.98 : renamed from CheckXFD
  67.  1.38  11.11.98 : fixed format drive bug using a delay and an error report
  68.  1.39  14.11.98 : format error with AUTOMOUNT removed
  69.  1.40  16.11.98 : removed HEADER addition stuff
  70.  1.41  18.11.98 : better RDx: access and mount
  71.  1.42  23.11.98 : fixed bug with hunk stripping
  72.  1.43  29.12.98 : now prints an error, when virus detection is turned off,
  73.     added time calculation and output
  74.  1.44  31.12.98 : little bug fix
  75.  1.45  06.02.99 : bug fixes, added xadmaster.library stuff, removed LOUD
  76.     and internal DMS call
  77.  1.46  09.02.99 : removed internal LZX call
  78.  1.47  11.02.99 : now uses assembler startcode allocating a bigger stack
  79.  1.48  14.02.99 : bug-fixes for nocylinder archivers (PackDev)
  80.  1.49  16.02.99 : fixed strip option (don't know, where the error was :-)
  81.  1.50  21.02.99 : removed internal Zoom call and disk-archiver stuff
  82.  1.51  22.02.99 : fixed archiver call for remaining non-XAD archivers, added
  83.     empty file check
  84.  1.52  23.02.99 : fixed StartCode return value
  85.  1.53  24.02.99 : forgot empty check for archived files
  86.  1.54  08.03.99 : old archiver calling did not work, when file was XAD
  87.     unarchived
  88.  1.55  26.03.99 : prints error, when logfile cannot be created
  89.  1.56  30.03.99 : added bootblock scanning for unarchived disks
  90.  1.57  16.05.99 : bug fix with unlinked file save
  91.  1.58  17.07.99 : removed internal LhA Support
  92.  1.59  30.07.99 : bug fix with file name prints
  93.  1.60  03.08.99 : added DEBUG option
  94.  1.61  05.08.99 : added XVS SelfTest and MemoryTest
  95.  1.62  19.08.99 : added error summary, again fixed name problem
  96.  1.63  14.09.99 : solved big memory loss problem (did not free xadArchiveInfo),
  97.     added logfile comment
  98.  1.64  17.10.99 : removed external archiver calls, added QUIET
  99.  1.65  24.11.99 : now sets nice return values
  100.  1.66  26.11.99 : tries reading again before giving read error
  101.  1.67  03.12.99 : bug fix in name printing
  102.  1.68  04.12.99 : tries again opening files.
  103.  1.69  16.12.99 : added NOSILENT Option
  104.  1.70  15.01.00 : files from disc archives are extracted directly now,
  105.     removed double read tries
  106.  1.71  16.01.00 : fixed error messages for disk unarchiving
  107.  1.72  12.02.00 : added support for ADF images
  108.  1.73  13.02.00 : bug fix for disk archives
  109.  1.74  09.03.00 : now detects XADFIF_NOFILENAME and XADFIF_NOUNCRUNCHSIZE
  110.  1.75  05.04.00 : fixed bug with XADFIF_NOFILENAME
  111.  1.76  26.04.00 : added size checkout for XADFIF_NOUNCRUNCHSIZE
  112.  1.77  05.06.00 : added support for multiple filesystems, removed NODOS and
  113.     diskerr errors, always prints FS-type now.
  114.  1.78  24.07.00 : bug fix
  115.  1.79  27.07.00 : bug fix with multi FS support
  116. */
  117.  
  118. #include <proto/exec.h>
  119. #include <proto/dos.h>
  120. #include <proto/xfdmaster.h>
  121. #include <proto/xadmaster.h>
  122. #include <proto/xpkmaster.h>
  123. #include <proto/intuition.h>
  124. #include <proto/xvs.h>
  125. #include <proto/utility.h>
  126. #include <libraries/xfdmaster.h>
  127. #include <dos/dostags.h>
  128. #include <dos/doshunks.h>
  129. #include <dos/filehandler.h>
  130. #include <exec/memory.h>
  131. #include "SDI_version.h" /* make version string */
  132. #include "SDI_compiler.h"
  133. #define SDI_TO_ANSI
  134. #include "SDI_ASM_STD_protos.h"
  135.  
  136. #define PARAM   "FROM,LOG,SAVE/K,ALL/S,ASKPWD/S,PRINTALL/S,PRINTEXEC/S," \
  137.         "NODECRUNCH/S,NOUNLINK/S,NOUNARCHIVE/S,NOUNTRACK/S,"     \
  138.         "NOSILENT/S,NOSTRIP/S,NOVIRUS/S,DEBUG/S,QUIET/S"
  139.  
  140. void RawPutChar(ULONG c);
  141.  
  142. #ifdef __SASC
  143.   #pragma libcall SysBase RawPutChar 204 001
  144. #elif defined(__GNUC__)
  145.   #define RawPutChar(c) LP1NR(204, RawPutChar, ULONG, c, d0, , SysBase)
  146. #else
  147.   #pragma amicall(SysBase,0x204,RawPutChar(d0))
  148. #endif
  149.  
  150. void KPutC(ULONG c)
  151. {
  152.   RawPutChar(c);
  153. }
  154.  
  155. void KPrintf(STRPTR fmt, ...)
  156. {
  157.   RawDoFmt(fmt, &fmt + 1, (void (*)()) KPutC, 0);
  158. }
  159.  
  160. #ifdef __SASC
  161.   #define XpkBase    xpkbase
  162.   #define ASSIGN_XPK
  163.   #define IntuitionBase    intuitionbase
  164.   #define ASSIGN_INT
  165.   #define UtilityBase    utilitybase
  166.   #define ASSIGN_UTIL
  167. #else
  168.   struct Library *    XpkBase        = 0;
  169.   struct IntuitionBase *IntuitionBase    = 0;
  170.   struct UtilityBase *  UtilityBase     = 0;
  171.   #define ASSIGN_XPK    XpkBase = xpkbase;
  172.   #define ASSIGN_INT    IntuitionBase = intuitionbase;
  173.   #define ASSIGN_UTIL    UtilityBase = utilitybase;
  174. #endif
  175. struct xfdMasterBase *    xfdMasterBase    = 0;
  176. struct DosLibrary *    DOSBase        = 0;
  177. struct ExecBase *    SysBase        = 0;
  178. struct xvsBase *    xvsBase        = 0;
  179. struct xadMasterBase *    xadMasterBase    = 0;
  180.  
  181. struct Args {
  182.   STRPTR from;
  183.   STRPTR log;
  184.   STRPTR save;
  185.   ULONG  all;
  186.   ULONG  askpwd;
  187.   ULONG  printall;
  188.   ULONG  printexec;
  189.   ULONG  nodecrunch;
  190.   ULONG  nounlink;
  191.   ULONG  nounarchive;
  192.   ULONG  nountrack;
  193.   ULONG  nosilent;
  194.   ULONG  nostrip;
  195.   ULONG  novirus;
  196.   ULONG  debug;
  197.   ULONG     quiet;
  198. };
  199.  
  200. struct CrunchMemList {
  201.   struct CrunchMemList * cml_Next;
  202.   APTR             cml_MemoryRegion;
  203.   ULONG             cml_MemorySize;
  204. };
  205.  
  206. #define CHECKXFLAG_SAVE            (1<< 0)
  207. #define CHECKXFLAG_ALL            (1<< 1)
  208. #define CHECKXFLAG_ASKPWD        (1<< 2)
  209. #define CHECKXFLAG_PRINTALL        (1<< 3)
  210. #define CHECKXFLAG_PRINTEXEC        (1<< 4)
  211. #define CHECKXFLAG_NODECRUNCH        (1<< 5)
  212. #define CHECKXFLAG_NOUNLINK        (1<< 6)
  213. #define CHECKXFLAG_NOUNARCHIVE        (1<< 7)
  214. #define CHECKXFLAG_NOUNTRACK        (1<< 8)
  215. #define CHECKXFLAG_NOSTRIP        (1<< 9)
  216. #define CHECKXFLAG_DEBUG        (1<<10)
  217. #define CHECKXFLAG_QUIET        (1<<11)
  218.  
  219. #define CHECKXFLAG_XVSLIB        (1<<20)
  220.  
  221. #define CHKXCALLFLAGS    (CHECKXFLAG_SAVE|CHECKXFLAG_ALL|        \
  222.              CHECKXFLAG_ASKPWD|CHECKXFLAG_PRINTALL|        \
  223.              CHECKXFLAG_PRINTEXEC|CHECKXFLAG_NODECRUNCH|    \
  224.              CHECKXFLAG_NOUNLINK|CHECKXFLAG_NOUNARCHIVE|    \
  225.              CHECKXFLAG_NOUNTRACK|CHECKXFLAG_NOSTRIP|    \
  226.              CHECKXFLAG_DEBUG|CHECKXFLAG_QUIET|        \
  227.              CHECKXFLAG_XVSLIB)
  228.  
  229. #define CHKXSAVEFLAGS    (CHECKXFLAG_LINKED|CHECKXFLAG_CRUNCHED|        \
  230.              CHECKXFLAG_STRIPPED)
  231.  
  232. #define CHECKXFLAG_NAMEPRINTED        (1<<26)
  233. #define CHECKXFLAG_CRUNCHED        (1<<27)
  234. #define CHECKXFLAG_LINKED        (1<<28)
  235. #define CHECKXFLAG_ADDRESS        (1<<29)
  236. #define CHECKXFLAG_NOFREEMEM        (1<<30)
  237. #define CHECKXFLAG_STRIPPED        (1<<31)
  238.  
  239. #define CHXWARN_OFFSET        30
  240. #define XADERR_OFFSET        0x100
  241. #define XFDERR_OFFSET        0x200
  242.  
  243. #define CHKXERR_NOMEMORY    1
  244. #define CHKXERR_EXAMINEERR    2
  245. #define CHKXERR_OPENERR        3
  246. #define CHKXERR_READ        4
  247. #define CHKXERR_SCANERR        5
  248. #define CHKXERR_BREAK        6
  249. #define CHKXERR_OPENDIR        7
  250. #define CHKXERR_RESOURCE    8
  251. #define CHKXERR_NOBOOTVIRUS    9
  252. #define CHKXERR_WRITE        10
  253. #define CHKXERR_NOVIRUS        11
  254. #define CHKXERR_EMPTY        12
  255.  
  256. #define CHKXWARN_NOVIRUS    31
  257. #define CHKXWARN_XVSSELFTEST    32
  258. #define    CHKXWARN_MEMVIRUS    33
  259.  
  260. struct FileData {
  261.   struct CrunchMemList * fd_MemList;
  262.   STRPTR         fd_Name;
  263.   ULONG           fd_LogFileFH;
  264.   ULONG           fd_SaveDirL;
  265.   ULONG             fd_Flags;
  266.   ULONG             fd_NumVirus;
  267.   ULONG             fd_CHKXErrors;
  268.   ULONG             fd_XFDErrors;
  269.   ULONG             fd_XADErrors;
  270.   ULONG             fd_CorruptedArchives;
  271.   BYTE           fd_RecurseDepth;
  272.   UBYTE           fd_LinkNum;
  273.   UBYTE             fd_ArchiveDepth;
  274. };
  275.  
  276. static void SetLogComment(STRPTR);
  277. static LONG DoDirectoryScan(STRPTR, STRPTR, struct FileData *);
  278. static LONG DoFileOpen(struct FileData *);
  279. static LONG DoGetVirus(struct FileData *, APTR, ULONG);
  280. ASM(LONG) OutHookCheckX(REG(a0, struct Hook *hook), REG(a1, struct xadHookParam *hp));
  281. static LONG DoFileUnArchive(struct FileData *, APTR, ULONG);
  282. static LONG DoFileUnLink(struct FileData *, APTR, ULONG);
  283. static LONG DoFileUnCrunch(struct FileData *, APTR, ULONG);
  284. static LONG DoFileStrip(struct FileData *, APTR, ULONG);
  285. static void PrintCHKXFile(struct FileData *);
  286. static void PrintCHKXErr(struct FileData *, LONG);
  287. static void PrintCHKXTxt(struct FileData *, STRPTR, ...);
  288. static LONG AddCrunchMemList(struct FileData *, APTR, ULONG);
  289. static void FreeCrunchMemList(struct FileData *, APTR);
  290. static LONG SaveUncrFile(struct FileData *, APTR, ULONG);
  291. static ULONG OpenParentDir(struct FileData *);
  292. static ULONG OpenNewDir(struct FileData *, STRPTR);
  293.  
  294. /* All memory regions must be in mem list. All unneeded memory must be freed
  295.    as fast as possible (after unlinking, decrunching), as well as the
  296.    MemoryList structure.
  297.    
  298.    The program has a loop like scan routine system, which is called for
  299.    every file:
  300.  
  301.    A) Scan files, directories and sub directories and call following for
  302.       every file:
  303.    1) Check for viruses.
  304.    2) Test if it is an archive. When yes decrunch and start for every file
  305.       with point 1.
  306.    3) Test if file is linked. When yes unlink and call point 1 for both
  307.       parts.
  308.    4) Test if file is crunched. When yes, decrunch and start again with
  309.       point 1.
  310.    5) Try stripping useless stuff. When successful start with point 1 again.
  311.    6) Possibly save file (with SAVE option) or end loop here.
  312. */
  313.  
  314. /* main routine, do argument parsing */
  315. LONG start(void)
  316. {
  317.   LONG error = RETURN_FAIL;
  318.   struct DosLibrary *dosbase;
  319.   struct Process *task;
  320.  
  321.   SysBase = (*((struct ExecBase **) 4));
  322.  
  323.   /* test for WB and reply startup-message */
  324.   if(!(task = (struct Process *) FindTask(0))->pr_CLI)
  325.   {
  326.     WaitPort(&task->pr_MsgPort);
  327.     Forbid();
  328.     ReplyMsg(GetMsg(&task->pr_MsgPort));
  329.     return RETURN_FAIL;
  330.   }
  331.  
  332.   if((dosbase = (struct DosLibrary *) OpenLibrary("dos.library", 37)))
  333.   {
  334.     struct xfdMasterBase *xfdmasterbase;
  335.  
  336.     DOSBase = dosbase;
  337.     if((xfdmasterbase = (struct xfdMasterBase *) OpenLibrary("xfdmaster.library", 37)))
  338.     {
  339.       struct xadMasterBase *xadmasterbase;
  340.  
  341.       xfdMasterBase = xfdmasterbase;
  342.       if((xadmasterbase = (struct xadMasterBase *) OpenLibrary("xadmaster.library", 7)))
  343.       {
  344.         struct Args Args;
  345.         struct RDArgs *rda;
  346.  
  347.     xadMasterBase = xadmasterbase;
  348.         memset(&Args, 0, sizeof(struct Args));
  349.  
  350.         if((rda = (struct RDArgs *) AllocDosObject(DOS_RDARGS, 0)))
  351.         {
  352.           rda->RDA_ExtHelp =
  353.           "FROM        source file or directory - may contain patterns\n"
  354.           "LOG         log file name\n"
  355.           "SAVE        directory, where decrunched files are saved\n"
  356.           "ALL         scan deep into directories\n"
  357.           "ASKPWD      ask for password when needed (needs xpkmaster.library)\n"
  358.       "PRINTALL    print all filenames\n"
  359.           "PRINTEXEC   print names of all executable files\n"
  360.           "NODECRUNCH  do not decrunch files with xfdmaster\n"
  361.           "NOUNLINK    do not unlink files with xfdmaster\n"
  362.           "NOUNARCHIVE do not unarchive file archives\n"
  363.           "NOUNTRACK   do not unarchive track archives\n"
  364.           "NOSILENT    do not disable dos requests\n"
  365.           "NOSTRIP     do not strip useless hunks\n"
  366.           "NOVIRUS     do not scan with xvs.library for viruses\n"
  367.           "DEBUG       also output texts to serial debug engine\n"
  368.           "QUIET       do not output texts to console\n";
  369.  
  370.           if(ReadArgs(PARAM, (LONG *) &Args, rda))
  371.           {
  372.             ULONG flags = 0, log = 0;
  373.             struct xvsBase *xvsbase = 0;
  374.  
  375.             if(!Args.from)        Args.from = "#?";
  376.             if(Args.all)        flags |= CHECKXFLAG_ALL;
  377.             if(Args.save)        flags |= CHECKXFLAG_SAVE;
  378.             if(Args.askpwd)        flags |= CHECKXFLAG_ASKPWD;
  379.         if(Args.printall)        flags |= CHECKXFLAG_PRINTALL;
  380.         if(Args.printexec)        flags |= CHECKXFLAG_PRINTEXEC;
  381.             if(Args.nodecrunch)        flags |= CHECKXFLAG_NODECRUNCH;
  382.             if(Args.nounlink)         flags |= CHECKXFLAG_NOUNLINK;
  383.             if(Args.nounarchive)    flags |= CHECKXFLAG_NOUNARCHIVE;
  384.             if(Args.nountrack)        flags |= CHECKXFLAG_NOUNTRACK;
  385.             if(Args.nostrip)        flags |= CHECKXFLAG_NOSTRIP;
  386.             if(Args.debug)        flags |= CHECKXFLAG_DEBUG;
  387.             if(Args.quiet)        flags |= CHECKXFLAG_QUIET;
  388.  
  389.             if(!Args.novirus)
  390.             {
  391.               if((xvsbase = (struct xvsBase *) OpenLibrary("xvs.library", 33)))
  392.               {
  393.                 flags |= CHECKXFLAG_XVSLIB;
  394.                 xvsBase = xvsbase;
  395.               }
  396.         }
  397.  
  398.             if(!Args.log || (log = Open(Args.log, MODE_READWRITE)))
  399.             {
  400.           struct FileData fd;
  401.           APTR win;
  402.           ULONG s1 = 0, s2 = 0, msecs;
  403.           struct IntuitionBase *intuitionbase;
  404.  
  405.           win = task->pr_WindowPtr;
  406.           if(!Args.nosilent)
  407.             task->pr_WindowPtr = (APTR) -1;
  408.           /* prevent dos requests */
  409.  
  410.           if(log)
  411.           {
  412.             SetFileSize(log, 0, OFFSET_BEGINNING);
  413.             SetProtection(Args.log, FIBF_EXECUTE);
  414.             SetLogComment(Args.log);
  415.           }
  416.  
  417.           memset(&fd, 0, sizeof(struct FileData));
  418.               fd.fd_Flags = flags | CHECKXFLAG_NAMEPRINTED; /* for possible warnings display */
  419.               fd.fd_LogFileFH = log;
  420.  
  421.           if(!xvsbase)
  422.             PrintCHKXErr(&fd, CHKXWARN_NOVIRUS);
  423.           else
  424.           {
  425.                 struct xvsMemoryInfo *mi;
  426.  
  427.             if(!xvsSelfTest())
  428.               PrintCHKXErr(&fd, CHKXWARN_XVSSELFTEST);
  429.  
  430.                   if((mi = (struct xvsMemoryInfo *) xvsAllocObject(XVSOBJ_MEMORYINFO)))
  431.                   { 
  432.                     if(xvsSurveyMemory(mi))
  433.                 PrintCHKXErr(&fd, CHKXWARN_MEMVIRUS);
  434.  
  435.                     xvsFreeObject(mi);
  436.                   }
  437.           }
  438.  
  439.           if((intuitionbase = (struct IntuitionBase *) OpenLibrary("intuition.library", 37)))
  440.           {
  441.             ASSIGN_INT
  442.             CurrentTime(&s1, &msecs);
  443.           }    
  444.  
  445.           ++fd.fd_RecurseDepth;
  446.           error = DoDirectoryScan(Args.from, Args.save, &fd) ? RETURN_FAIL : RETURN_OK;
  447.           --fd.fd_RecurseDepth;
  448.           fd.fd_Flags |= CHECKXFLAG_NAMEPRINTED; /* for result texts */
  449.  
  450.           task->pr_WindowPtr = win;
  451.  
  452.           if(intuitionbase)
  453.           {
  454.             CurrentTime(&s2, &msecs);
  455.             s2 -= s1;
  456.             s1 = s2 / 60;
  457.             s2 %= 60;
  458.             msecs = s1 / 60;
  459.             s1 %= 60;
  460.             PrintCHKXTxt(&fd, "\nTime needed for check: %2ld:%02ld:%02ld", msecs, s1, s2);
  461.             CloseLibrary((struct Library *) intuitionbase);
  462.           }
  463.           if(fd.fd_NumVirus)
  464.             PrintCHKXTxt(&fd, "The scan detected %ld virus%s.", fd.fd_NumVirus, fd.fd_NumVirus > 1 ? "es" : "");
  465.           if(fd.fd_CHKXErrors || fd.fd_XADErrors || fd.fd_XFDErrors || fd.fd_CorruptedArchives)
  466.             PrintCHKXTxt(&fd, "There were errors (CheckX/XFD/XAD/corrupted archives): %ld/%ld/%ld/%ld.",
  467.             fd.fd_CHKXErrors, fd.fd_XFDErrors, fd.fd_XADErrors, fd.fd_CorruptedArchives);
  468.           if(!error && (fd.fd_NumVirus || !xvsBase))
  469.           {
  470.             SetIoErr(0);
  471.             error = RETURN_WARN;
  472.           }
  473.  
  474.               if(log)
  475.                 Close(log);
  476.             }
  477.             else if(Args.log && !Args.quiet)
  478.               Printf("Could not create logfile.\n");
  479.  
  480.         if(xvsbase)
  481.           CloseLibrary((struct Library *) xvsbase);
  482.             FreeArgs(rda);
  483.           }
  484.           FreeDosObject(DOS_RDARGS, rda);
  485.         }
  486.     CloseLibrary((struct Library *) xadmasterbase);
  487.       }
  488.       CloseLibrary((struct Library *) xfdmasterbase);
  489.     }
  490.     CloseLibrary((struct Library *) dosbase);
  491.   }
  492.  
  493.   return error;
  494. }
  495.  
  496. static void SetLogComment(STRPTR name)
  497. {
  498.   UBYTE com[30];
  499.   struct MsgPort *TimerMP;
  500.  
  501.   if((TimerMP = CreateMsgPort()))
  502.   {
  503.     struct timerequest *TimerIO;
  504.  
  505.     if((TimerIO = (struct timerequest *) CreateIORequest(TimerMP,
  506.     sizeof(struct timerequest))))
  507.     {
  508.       if(!OpenDevice("timer.device",UNIT_VBLANK,
  509.       (struct IORequest *)TimerIO,0))
  510.       {
  511.         struct UtilityBase *utilitybase;
  512.  
  513.         TimerIO->tr_node.io_Command = TR_GETSYSTIME;
  514.         DoIO((struct IORequest *) TimerIO);
  515.         if((utilitybase = (struct UtilityBase *) OpenLibrary("utility.library",37)))
  516.         {
  517.           struct ClockData dat;
  518.  
  519.       ASSIGN_UTIL
  520.           Amiga2Date(TimerIO->tr_time.tv_secs, &dat);
  521.           sprintf(com, "CheckX " VERSION "." REVISION " - %02ld.%02ld.%ld", dat.mday, dat.month, dat.year);
  522.       CloseLibrary((struct Library *) utilitybase);
  523.       SetComment(name, com);
  524.         }
  525.         CloseDevice((struct IORequest *) TimerIO);
  526.       }
  527.       DeleteIORequest(TimerIO);
  528.     }
  529.     DeleteMsgPort(TimerMP);
  530.   }
  531. }
  532.  
  533. /* This scans a directory and calls DoFileOpen for every file. It
  534.    automatically creates SAVE destination directories when necessary. */
  535. static LONG DoDirectoryScan(STRPTR name, STRPTR sav, struct FileData *fd)
  536. {
  537.   struct AnchorPath *APath;
  538.   LONG error = CHKXERR_SCANERR;
  539.   ULONG retval;
  540.  
  541.   if(!(fd->fd_Flags & CHECKXFLAG_SAVE) || !sav ||
  542.   (fd->fd_SaveDirL = Lock(sav, SHARED_LOCK)))
  543.   {
  544.     if((APath = (struct AnchorPath *) AllocMem(sizeof(struct AnchorPath) +
  545.     512, MEMF_PUBLIC|MEMF_CLEAR)))
  546.     {
  547.       fd->fd_Name = APath->ap_Buf;
  548.       APath->ap_Strlen = 256;
  549.       for(retval = MatchFirst(name, APath); !retval;
  550.       retval = MatchNext(APath))
  551.       {
  552.         if(APath->ap_Flags & APF_DIDDIR)
  553.         {
  554.           OpenParentDir(fd);
  555.           APath->ap_Flags &= ~APF_DIDDIR; /* clear flag */
  556.         }
  557.         else if(APath->ap_Info.fib_DirEntryType > 0)
  558.         {
  559.           if(fd->fd_Flags & CHECKXFLAG_ALL)
  560.           {
  561.             OpenNewDir(fd, APath->ap_Info.fib_FileName);
  562.             APath->ap_Flags |= APF_DODIR;
  563.           }
  564.         }
  565.         else
  566.         {
  567.       fd->fd_Flags &= CHKXCALLFLAGS;
  568.           fd->fd_LinkNum = 0;
  569.           PrintCHKXErr(fd, DoFileOpen(fd));
  570.  
  571.           while(fd->fd_MemList)
  572.             FreeCrunchMemList(fd, fd->fd_MemList->cml_MemoryRegion);
  573.         }
  574.         if((fd->fd_Flags & CHECKXFLAG_SAVE) && !fd->fd_SaveDirL)
  575.         {
  576.           error = CHKXERR_OPENDIR; break;
  577.         }
  578.         if(SetSignal(0L,0L) & SIGBREAKF_CTRL_C)
  579.         {
  580.           error = CHKXERR_BREAK; break;
  581.         }
  582.       }
  583.       MatchEnd(APath);
  584.  
  585.       if(retval == ERROR_NO_MORE_ENTRIES)
  586.         error = 0;
  587.  
  588.       FreeMem(APath, sizeof(struct AnchorPath) + 512);
  589.     }
  590.     else
  591.       error = CHKXERR_NOMEMORY;
  592.  
  593.     if(sav && fd->fd_SaveDirL)
  594.       UnLock(fd->fd_SaveDirL);
  595.   }
  596.   else
  597.     error = CHKXERR_OPENDIR;
  598.  
  599.   fd->fd_Flags |= CHECKXFLAG_NAMEPRINTED;
  600.   if(error)
  601.     PrintCHKXErr(fd, error);
  602.  
  603.   return error;
  604. }
  605.  
  606. /* Open a file and call DoGetVirus to scan */
  607. static LONG DoFileOpen(struct FileData *fd)
  608. {
  609.   struct FileInfoBlock *fib;
  610.   LONG ret = 0;
  611.  
  612.   if((fib = (struct FileInfoBlock *) AllocDosObject(DOS_FIB, 0)))
  613.   {
  614.     ULONG fh;
  615.  
  616.     if((fh = Open(fd->fd_Name, MODE_OLDFILE)))
  617.     {
  618.       if(ExamineFH(fh, fib))
  619.       {
  620.         APTR mem;
  621.  
  622.     if(!fib->fib_Size)
  623.           ret = CHKXERR_EMPTY;
  624.         else if((mem = AllocMem(fib->fib_Size, MEMF_ANY)))
  625.         {
  626.           if(Read(fh, mem, fib->fib_Size) != fib->fib_Size)
  627.             ret = CHKXERR_READ;
  628.  
  629.           if(!ret && !(ret = AddCrunchMemList(fd, mem, fib->fib_Size)))
  630.           {
  631.             if((fd->fd_Flags & CHECKXFLAG_PRINTALL) ||
  632.             ((fd->fd_Flags & CHECKXFLAG_PRINTEXEC) && fib->fib_Size > 4
  633.             && *((ULONG *)mem) == HUNK_HEADER))
  634.               PrintCHKXFile(fd);
  635.             ret = DoGetVirus(fd, mem, fib->fib_Size);
  636.           }
  637.           else
  638.             FreeMem(mem, fib->fib_Size);
  639.         }
  640.         else
  641.           ret = CHKXERR_NOMEMORY;
  642.       }
  643.       else
  644.         ret = CHKXERR_EXAMINEERR;
  645.       Close(fh);
  646.     }
  647.     else
  648.       ret = CHKXERR_OPENERR;
  649.   
  650.     FreeDosObject(DOS_FIB, fib);
  651.   }
  652.   else
  653.     ret = CHKXERR_NOMEMORY;
  654.  
  655.   return ret;
  656. }
  657.  
  658. /* for SAVE option: open parent directory and try to delete the directory
  659.    we leave. This only succeeds, when the directory is empty. */
  660. static ULONG OpenParentDir(struct FileData *fd)
  661. {
  662.   ULONG g;
  663.   UBYTE name[300];
  664.  
  665.   if((g = fd->fd_SaveDirL))
  666.   {
  667.     NameFromLock(g, name, 300);
  668.     fd->fd_SaveDirL = ParentDir(g);
  669.     UnLock(g);
  670.  
  671.     DeleteFile(name);
  672.   }
  673.   else
  674.     return -1;
  675.  
  676.   return fd->fd_SaveDirL;
  677. }
  678.  
  679. /* Open a new subdirectory for SAVE option */
  680. static ULONG OpenNewDir(struct FileData *fd, STRPTR name)
  681. {
  682.   ULONG g;
  683.  
  684.   if(fd->fd_SaveDirL)
  685.   {
  686.     g = CurrentDir(fd->fd_SaveDirL);
  687.     if(!(fd->fd_SaveDirL = Lock(name, SHARED_LOCK)))
  688.       if((fd->fd_SaveDirL = CreateDir(name)))
  689.         ChangeMode(CHANGE_LOCK, fd->fd_SaveDirL, SHARED_LOCK);
  690.     UnLock(CurrentDir(g));
  691.   }
  692.   else
  693.     return -1;
  694.  
  695.   return fd->fd_SaveDirL;
  696. }
  697.  
  698. static LONG DoGetVirus(struct FileData *fd, APTR buffer, ULONG buflength)
  699. {
  700.   if(!buflength) /* restarted with empty file */
  701.     return CHKXERR_EMPTY;
  702.  
  703.   if(fd->fd_Flags & CHECKXFLAG_XVSLIB)
  704.   {
  705.     APTR mem;
  706.       
  707.     if((mem = AllocMem(buflength, MEMF_ANY)))
  708.     {
  709.       struct xvsFileInfo *fi;
  710.       if((fi = (struct xvsFileInfo *) xvsAllocObject(XVSOBJ_FILEINFO)))
  711.       {
  712.         ULONG i;
  713.     /* xvs may modify the buffer! */
  714.         CopyMem(buffer, mem, buflength);
  715.         fi->xvsfi_File = mem;
  716.         fi->xvsfi_FileLen = buflength;
  717.         i = xvsCheckFile(fi);
  718.         if(i == XVSFT_DATAVIRUS)
  719.         {
  720.        PrintCHKXTxt(fd, "Data-Virus '%s'", fi->xvsfi_Name);
  721.        ++fd->fd_NumVirus;
  722.      }
  723.         else if(i == XVSFT_FILEVIRUS)
  724.         {
  725.        PrintCHKXTxt(fd, "File-Virus '%s'", fi->xvsfi_Name);
  726.        ++fd->fd_NumVirus;
  727.      }
  728.         else if(i == XVSFT_LINKVIRUS)
  729.         {
  730.        PrintCHKXTxt(fd, "Link-Virus '%s'", fi->xvsfi_Name);
  731.        ++fd->fd_NumVirus;
  732.      }
  733.         xvsFreeObject(fi);
  734.       }
  735.       else
  736.         PrintCHKXErr(fd, CHKXERR_NOVIRUS);
  737.       FreeMem(mem, buflength);
  738.     }
  739.     else
  740.       PrintCHKXErr(fd, CHKXERR_NOVIRUS);
  741.   }
  742.  
  743.   return DoFileUnArchive(fd, buffer, buflength);
  744. }
  745.  
  746. /* For bootblocks */
  747. ASM(LONG) OutHookCheckX(REG(a0, struct Hook *hook), REG(a1, struct xadHookParam *hp))
  748. {
  749.   ULONG i;
  750.  
  751.   switch(hp->xhp_Command)
  752.   {
  753.   case XADHC_WRITE:
  754.     if((i = 1024-hp->xhp_DataPos) > hp->xhp_BufferSize)
  755.       i = hp->xhp_BufferSize;
  756.     CopyMem(hp->xhp_BufferPtr, ((STRPTR)(hook->h_Data))+hp->xhp_DataPos, i);
  757.     hp->xhp_DataPos += i;
  758.     if(hp->xhp_DataPos == 1024)
  759.       return 10000; /* CheckX own error code for error detection */
  760.   case XADHC_INIT:
  761.   case XADHC_FREE:
  762.   case XADHC_ABORT:
  763.     break;
  764.   default: return XADERR_NOTSUPPORTED;
  765.   }
  766.  
  767.   return 0;
  768. }
  769.  
  770. /* To get real file size */
  771. ASM(LONG) OutHookCheckXSize(REG(a0, struct Hook *hook), REG(a1, struct xadHookParam *hp))
  772. {
  773.   switch(hp->xhp_Command)
  774.   {
  775.   case XADHC_WRITE:
  776.     hp->xhp_DataPos += hp->xhp_BufferSize;
  777.     hook->h_Data = (APTR) hp->xhp_DataPos;
  778.   case XADHC_INIT:
  779.   case XADHC_FREE:
  780.   case XADHC_ABORT:
  781.     break;
  782.   default: return XADERR_NOTSUPPORTED;
  783.   }
  784.  
  785.   return 0;
  786. }
  787.  
  788. static void CheckArcFiles(struct FileData *fd, struct xadArchiveInfo *ai, ULONG disk)
  789. {
  790.   struct xadFileInfo *fi;
  791.   struct TagItem ti[4];
  792.  
  793.   ti[0].ti_Tag = XAD_OUTMEMORY;
  794.   ti[1].ti_Tag = XAD_OUTSIZE;
  795.   ti[2].ti_Tag = XAD_ENTRYNUMBER;
  796.   ti[3].ti_Tag = TAG_DONE;
  797.  
  798.   fi = ai->xai_FileInfo;
  799.   while(!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C) && fi)
  800.   {
  801.     APTR dest;
  802.  
  803.     if(!(fi->xfi_Flags & (XADFIF_DIRECTORY|XADFIF_LINK)))
  804.     {
  805.       LONG err = 0;
  806.       ULONG flags, size;
  807.  
  808.       flags = fd->fd_Flags;
  809.       if(!(fi->xfi_Flags & XADFIF_NOFILENAME))
  810.         fd->fd_Flags &= ~CHECKXFLAG_NAMEPRINTED;
  811.       fd->fd_Name = fi->xfi_FileName;
  812.       fd->fd_RecurseDepth++;
  813.       fd->fd_ArchiveDepth++;
  814.  
  815.       size = fi->xfi_Size;
  816.       if(fi->xfi_Flags & XADFIF_NOUNCRUNCHSIZE)
  817.       {
  818.     struct Hook hook = {{0,0},(ULONG (*)()) OutHookCheckXSize, 0, 0};
  819.       struct TagItem ti[3];
  820.  
  821.     ti[0].ti_Tag = XAD_OUTHOOK;
  822.     ti[0].ti_Data = (ULONG) &hook;
  823.       ti[1].ti_Tag = XAD_ENTRYNUMBER;
  824.     ti[1].ti_Data = fi->xfi_EntryNumber;
  825.       ti[2].ti_Tag = TAG_DONE;
  826.  
  827.         if(!(err = (disk ? xadDiskFileUnArcA(ai, ti) : xadFileUnArcA(ai, ti))))
  828.       size = (ULONG) hook.h_Data;
  829.       }
  830.  
  831.       if(!size)
  832.       {
  833.         if(!err)
  834.           err = CHKXERR_EMPTY;
  835.       }
  836.       else if((dest = AllocMem(size, MEMF_PUBLIC)))
  837.       {
  838.         if(!(err = AddCrunchMemList(fd, dest, size)))
  839.         {
  840.       ti[0].ti_Data = (ULONG) dest;
  841.       ti[1].ti_Data = size;
  842.       ti[2].ti_Data = fi->xfi_EntryNumber;
  843.  
  844.           if((err = disk ? xadDiskFileUnArcA(ai, ti) : xadFileUnArcA(ai, ti)))
  845.       {
  846.         if(disk)
  847.           err = CHKXERR_READ;
  848.         else
  849.               err += XADERR_OFFSET;
  850.           }
  851.           else
  852.           {
  853.             if((fd->fd_Flags & CHECKXFLAG_PRINTALL) ||
  854.         ((fd->fd_Flags & CHECKXFLAG_PRINTEXEC) && size > 4
  855.         && *((ULONG *)dest) == HUNK_HEADER))
  856.               PrintCHKXFile(fd);
  857.             err = DoGetVirus(fd, dest, size);
  858.           }
  859.           FreeCrunchMemList(fd, dest);
  860.         }
  861.         else
  862.           FreeMem(dest, size);
  863.       }
  864.       else
  865.         err = CHKXERR_NOMEMORY;
  866.  
  867.       if(err)
  868.       {
  869.         if(fi->xfi_Flags & XADFIF_NOFILENAME)
  870.           fd->fd_Flags &= ~CHECKXFLAG_NAMEPRINTED;
  871.         PrintCHKXErr(fd, err);
  872.       }
  873.  
  874.       --fd->fd_RecurseDepth;
  875.       --fd->fd_ArchiveDepth;
  876.       fd->fd_Flags = flags;
  877.     }
  878.     fi = fi->xfi_Next;
  879.   }
  880. }
  881.  
  882. /* Tests if a file is an archive. When yes, the archive is unarchived and
  883.    the result files are scanned. Else DoFileUnLink is called.
  884. */
  885. static LONG DoFileUnArchive(struct FileData *fd, APTR buffer, ULONG buflength)
  886. {
  887.   LONG err = 0;
  888.   struct xadArchiveInfo *ai;
  889.  
  890.   if((ai = (struct xadArchiveInfo *) xadAllocObject(XADOBJ_ARCHIVEINFO, 0)))
  891.   {
  892.     STRPTR arcname;
  893.  
  894.     arcname = fd->fd_Name;
  895.     if(!xadGetInfo(ai, XAD_INMEMORY, buffer, XAD_INSIZE, buflength,
  896.     TAG_DONE))
  897.     {
  898.       if(ai->xai_Flags & XADAIF_FILECORRUPT)
  899.       {
  900.         ++fd->fd_CorruptedArchives;
  901.         PrintCHKXTxt(fd, "%s (ARCHIVE, CORRUPTED)", ai->xai_Client->xc_ArchiverName);
  902.       }
  903.       else
  904.         PrintCHKXTxt(fd, "%s (ARCHIVE)", ai->xai_Client->xc_ArchiverName);
  905.         
  906.       if(!(!(fd->fd_Flags & CHECKXFLAG_NOUNARCHIVE) && ai->xai_FileInfo ||
  907.       !(fd->fd_Flags & CHECKXFLAG_NOUNTRACK) && ai->xai_DiskInfo))
  908.         err = DoFileUnLink(fd, buffer, buflength);
  909.       else if(!OpenNewDir(fd, FilePart(fd->fd_Name)))
  910.         err = CHKXERR_OPENERR;
  911.       else
  912.       {
  913.     if(!(fd->fd_Flags & CHECKXFLAG_NOUNARCHIVE) && ai->xai_FileInfo)
  914.       CheckArcFiles(fd, ai, 0);
  915.     if(!(fd->fd_Flags & CHECKXFLAG_NOUNTRACK) && ai->xai_DiskInfo)
  916.     {
  917.       STRPTR buf;
  918.  
  919.       /* reduce stacksize, as we have a recursive program */
  920.       if((buf = (STRPTR) AllocMem(1024, MEMF_PUBLIC)))
  921.       {
  922.             ULONG flags;
  923.         struct xadDiskInfo *di;
  924.         struct xadArchiveInfo *aid;
  925.         di = ai->xai_DiskInfo;
  926.  
  927.             flags = fd->fd_Flags;
  928.         while(!(SetSignal(0L,0L) & SIGBREAKF_CTRL_C) && di)
  929.         {
  930.           if(di->xdi_SectorSize != 512 || (di->xdi_TrackSectors != 11 &&
  931.           di->xdi_TrackSectors != 22) || (!(di->xdi_Flags & XADDIF_NOHEADS) &&
  932.           di->xdi_Heads != 2) || (!(di->xdi_Flags & XADDIF_NOCYLINDERS) &&
  933.           di->xdi_Cylinders != 80))
  934.             sprintf(buf, "-disk image %ld", di->xdi_EntryNumber);
  935.           else
  936.           {
  937.             if(!(di->xdi_Flags & (XADDIF_NOHIGHCYL|XADDIF_NOLOWCYL)) &&
  938.          (di->xdi_LowCyl || di->xdi_HighCyl != 79))
  939.                   sprintf(buf, "-disk image %ld (%s, %ld to %ld)",
  940.                   di->xdi_EntryNumber, di->xdi_TrackSectors == 22 ?
  941.                   "HD" : "DD", di->xdi_LowCyl, di->xdi_HighCyl);
  942.                 else
  943.                   sprintf(buf, "-disk image %ld (%s)",
  944.                   di->xdi_EntryNumber, di->xdi_TrackSectors == 22 ? "HD" : "DD");
  945.               }
  946.       
  947.               fd->fd_Flags &= ~CHECKXFLAG_NAMEPRINTED;
  948.           fd->fd_Name = buf;
  949.               PrintCHKXFile(fd);
  950.  
  951.           if(di->xdi_TextInfo)
  952.           {
  953.                 struct xadTextInfo *ti;
  954.                 ULONG flags, i = 1;
  955.  
  956.             for(ti = di->xdi_TextInfo; ti; ti = ti->xti_Next)
  957.             {
  958.               if(ti->xti_Size && ti->xti_Text)
  959.               {
  960.                     flags = fd->fd_Flags;
  961.                     fd->fd_Flags &= ~CHECKXFLAG_NAMEPRINTED;
  962.                     fd->fd_Flags |= CHECKXFLAG_NOFREEMEM;
  963.                     fd->fd_RecurseDepth++;
  964.                     sprintf(buf, "--infotext %ld (size %ld)", i, ti->xti_Size);
  965.                     if((fd->fd_Flags & CHECKXFLAG_PRINTALL) ||
  966.                     ((fd->fd_Flags & CHECKXFLAG_PRINTEXEC) && ti->xti_Size > 4
  967.                     && *((ULONG *)(ti->xti_Text)) == HUNK_HEADER))
  968.                       PrintCHKXFile(fd);
  969.                     PrintCHKXErr(fd, DoGetVirus(fd, ti->xti_Text, ti->xti_Size));
  970.                     --fd->fd_RecurseDepth;
  971.                     fd->fd_Flags = flags;
  972.                   }
  973.                   ++i;
  974.                 }
  975.               }
  976.  
  977.           /* check bootblock here - special outhook for bootblock extraction */
  978.           if(!di->xdi_LowCyl) /* boot check only, when first block! */
  979.           {
  980.             struct Hook hook = {{0,0},(ULONG (*)()) OutHookCheckX, 0};
  981.  
  982.             hook.h_Data = buf;
  983.             if(xadDiskUnArc(ai, XAD_ENTRYNUMBER, di->xdi_EntryNumber,
  984.             XAD_OUTHOOK, &hook, TAG_DONE) == 10000) /* special hook return code! */
  985.             {
  986.               struct xvsBootInfo *bi;
  987.  
  988.               if((bi = (struct xvsBootInfo *) xvsAllocObject(XVSOBJ_BOOTINFO)))
  989.                 {
  990.                 bi->xvsbi_Bootblock = buf;
  991.                 if(xvsCheckBootblock(bi) == XVSBT_VIRUS)
  992.                 {
  993.                   PrintCHKXTxt(fd, "Boot-Virus '%s'", bi->xvsbi_Name);
  994.                   ++fd->fd_NumVirus;
  995.                 }
  996.                 xvsFreeObject(bi);
  997.                 }
  998.                 else
  999.                   PrintCHKXErr(fd, CHKXERR_NOBOOTVIRUS);
  1000.               }
  1001.               else
  1002.                 PrintCHKXErr(fd, CHKXERR_NOBOOTVIRUS);
  1003.           }
  1004.     
  1005.           if((aid = (struct xadArchiveInfo *) xadAllocObject(XADOBJ_ARCHIVEINFO, 0)))
  1006.           {
  1007.             struct TagItem ti[4];
  1008.             struct xadClient *cl;
  1009.       
  1010.           ti[0].ti_Tag = XAD_INMEMORY;
  1011.           ti[0].ti_Data = (ULONG) buffer;
  1012.           ti[1].ti_Tag = XAD_INSIZE;
  1013.           ti[1].ti_Data = buflength;
  1014.           ti[2].ti_Tag = XAD_ENTRYNUMBER;
  1015.           ti[2].ti_Data = di->xdi_EntryNumber;
  1016.           ti[3].ti_Tag = TAG_DONE;
  1017.     
  1018.           if(!(err = xadGetDiskInfo(aid, XAD_NOEMPTYERROR, 1, XAD_INDISKARCHIVE, ti, TAG_DONE)))
  1019.           {
  1020.             if(aid->xai_Flags & XADAIF_FILECORRUPT)
  1021.             {
  1022.                     ++fd->fd_CorruptedArchives;
  1023.                     PrintCHKXTxt(fd, "%s (FILESYSTEM, CORRUPTED)", aid->xai_Client->xc_ArchiverName);
  1024.             }
  1025.                   else
  1026.                     PrintCHKXTxt(fd, "%s (FILESYSTEM)", aid->xai_Client->xc_ArchiverName);
  1027.           CheckArcFiles(fd, aid, 1);
  1028.           cl = aid->xai_Client->xc_Next;
  1029.           xadFreeInfo(aid);
  1030.           while(cl) /* multiple filesystems */
  1031.           {
  1032.               if(!xadGetDiskInfo(aid, XAD_NOEMPTYERROR, 1, XAD_INDISKARCHIVE, ti, XAD_STARTCLIENT, cl, TAG_DONE))
  1033.               {
  1034.                 if(aid->xai_Flags & XADAIF_FILECORRUPT)
  1035.                 {
  1036.                         ++fd->fd_CorruptedArchives;
  1037.                         PrintCHKXTxt(fd, "%s (FILESYSTEM, CORRUPTED)", aid->xai_Client->xc_ArchiverName);
  1038.                 }
  1039.                       else
  1040.                         PrintCHKXTxt(fd, "%s (FILESYSTEM)", aid->xai_Client->xc_ArchiverName);
  1041.               CheckArcFiles(fd, aid, 1);
  1042.               cl = aid->xai_Client->xc_Next;
  1043.               xadFreeInfo(aid);
  1044.             }
  1045.             else
  1046.               cl = 0;
  1047.           }
  1048.           }
  1049.           else
  1050.           {
  1051.             if(err != XADERR_FILESYSTEM)
  1052.               PrintCHKXErr(fd, XADERR_OFFSET + err);
  1053.             err = 0;
  1054.           }
  1055.           xadFreeObjectA(aid, 0);
  1056.           }
  1057.  
  1058.               di = di->xdi_Next;
  1059.             } /* while */
  1060.             fd->fd_Flags = flags;
  1061.             FreeMem(buf, 1024);
  1062.           } /* AllocMem buffer */
  1063.           else
  1064.             err = CHKXERR_NOMEMORY;
  1065.         } /* is there a disk entry? */
  1066.         if(!OpenParentDir(fd))
  1067.           err = CHKXERR_OPENDIR;
  1068.       }
  1069.  
  1070.       xadFreeInfo(ai);
  1071.     } /* xadGetInfo */
  1072.     else if(!xadGetDiskInfo(ai, XAD_NOEMPTYERROR, 1, XAD_INMEMORY, buffer, XAD_INSIZE, buflength, TAG_DONE))
  1073.     {
  1074.       struct xadClient *cl;
  1075.       ULONG isvalid = 1;
  1076.  
  1077.       if(ai->xai_Flags & XADAIF_FILECORRUPT)
  1078.       {
  1079.         ++fd->fd_CorruptedArchives;
  1080.         PrintCHKXTxt(fd, "%s (FILESYSTEM, CORRUPTED)", ai->xai_Client->xc_ArchiverName);
  1081.       }
  1082.       else
  1083.         PrintCHKXTxt(fd, "%s (FILESYSTEM)", ai->xai_Client->xc_ArchiverName);
  1084.         
  1085.       if(fd->fd_Flags & CHECKXFLAG_NOUNTRACK)
  1086.         err = DoFileUnLink(fd, buffer, buflength);
  1087.       else if(!OpenNewDir(fd, FilePart(fd->fd_Name)))
  1088.     err = CHKXERR_OPENERR;
  1089.       else
  1090.       {
  1091.     CheckArcFiles(fd, ai, 1);
  1092.     while(isvalid && (cl = ai->xai_Client->xc_Next))
  1093.     {
  1094.           xadFreeInfo(ai);
  1095.           if(!xadGetDiskInfo(ai, XAD_NOEMPTYERROR, 1, XAD_INMEMORY, buffer, XAD_INSIZE, buflength, XAD_STARTCLIENT, cl, TAG_DONE))
  1096.           {
  1097.             if(ai->xai_Flags & XADAIF_FILECORRUPT)
  1098.             {
  1099.               ++fd->fd_CorruptedArchives;
  1100.               PrintCHKXTxt(fd, "%s (FILESYSTEM, CORRUPTED)", ai->xai_Client->xc_ArchiverName);
  1101.             }
  1102.             else
  1103.               PrintCHKXTxt(fd, "%s (FILESYSTEM)", ai->xai_Client->xc_ArchiverName);
  1104.         CheckArcFiles(fd, ai, 1);
  1105.           }
  1106.           else
  1107.             isvalid = 0;
  1108.         } 
  1109.         if(!OpenParentDir(fd))
  1110.           err = CHKXERR_OPENDIR;
  1111.       }
  1112.       if(isvalid)
  1113.         xadFreeInfo(ai);
  1114.     }
  1115.     else
  1116.       err = DoFileUnLink(fd, buffer, buflength);
  1117.  
  1118.     fd->fd_Name = arcname;
  1119.     xadFreeObjectA(ai, 0);
  1120.   }
  1121.   else
  1122.     err = CHKXERR_NOMEMORY;
  1123.  
  1124.   return err;
  1125. }
  1126.  
  1127. /* Tries to unlink a file. When the file was linked, we call DoGetVirus
  1128.    for the two parts to check if they may be archives, else we call
  1129.    DoFileUnCrunch.
  1130. */
  1131. static LONG DoFileUnLink(struct FileData *fd, APTR buffer, ULONG buflength)
  1132. {
  1133.   LONG ret = CHKXERR_NOMEMORY;
  1134.   struct xfdLinkerInfo *xli;
  1135.  
  1136.   if((xli = (struct xfdLinkerInfo *) xfdAllocObject(XFDOBJ_LINKERINFO)))
  1137.   {
  1138.     xli->xfdli_Buffer = buffer;
  1139.     xli->xfdli_BufLen = buflength;
  1140.     if(xfdRecogLinker(xli))
  1141.     {
  1142.       PrintCHKXTxt(fd, xli->xfdli_LinkerName);
  1143.       if(fd->fd_Flags & CHECKXFLAG_NOUNLINK)
  1144.         ret = DoFileUnCrunch(fd, buffer, buflength);
  1145.       else if(xfdUnlink(xli))
  1146.       {
  1147.         ULONG flags;
  1148.     fd->fd_Flags |= CHECKXFLAG_LINKED;
  1149.         ++fd->fd_RecurseDepth;
  1150.         ++fd->fd_LinkNum;
  1151.     flags = fd->fd_Flags;
  1152.     fd->fd_Flags |= CHECKXFLAG_NOFREEMEM;
  1153.         PrintCHKXErr(fd, DoGetVirus(fd, xli->xfdli_Save1,
  1154.         xli->xfdli_SaveLen1));
  1155.         fd->fd_Flags = flags; /* CHECKXFLAG_NOFREEMEM is cleared */
  1156.         PrintCHKXErr(fd, DoGetVirus(fd, xli->xfdli_Save2,
  1157.         xli->xfdli_SaveLen2));
  1158.         ret = 0;
  1159.         --fd->fd_RecurseDepth;
  1160.       }
  1161.       else
  1162.         ret = XFDERR_OFFSET + xli->xfdli_Error;
  1163.     }
  1164.     else
  1165.       ret = DoFileUnCrunch(fd, buffer, buflength);
  1166.  
  1167.     xfdFreeObject(xli);
  1168.   }
  1169.   return ret;
  1170. }
  1171.  
  1172. /* Tries to decrunch a file. When it is crunched, we decrunch it and call
  1173.    DoGetVirus to start the loop again. Else we call unstripping.
  1174. */
  1175. static LONG DoFileUnCrunch(struct FileData *fd, APTR buffer, ULONG buflength)
  1176. {
  1177.   LONG ret = CHKXERR_NOMEMORY;
  1178.   struct xfdBufferInfo *xbi;
  1179.  
  1180.   if((xbi = (struct xfdBufferInfo *) xfdAllocObject(XFDOBJ_BUFFERINFO)))
  1181.   {
  1182.     xbi->xfdbi_SourceBuffer = buffer;
  1183.     xbi->xfdbi_SourceBufLen = buflength;
  1184.     xbi->xfdbi_Flags = XFDFF_RECOGEXTERN;
  1185.     if(xfdRecogBuffer(xbi))
  1186.     {
  1187.       struct Library *xpkbase;
  1188.       STRPTR buf = 0;
  1189.       ULONG buflen = 0;
  1190.  
  1191.       PrintCHKXTxt(fd, xbi->xfdbi_PackerFlags & XFDPFF_ADDR ? "%s (ADDRESS)" :
  1192.       "%s", xbi->xfdbi_PackerName);
  1193.  
  1194.       if(fd->fd_Flags & CHECKXFLAG_ASKPWD && (xpkbase =
  1195.       OpenLibrary(XPKNAME, 4)))
  1196.       {
  1197.     ASSIGN_XPK
  1198.         if(xbi->xfdbi_PackerFlags & XFDPFF_PASSWORD)
  1199.         {
  1200.           buflen = (xbi->xfdbi_MaxSpecialLen == 0xFFFF) ? 256 :
  1201.         xbi->xfdbi_MaxSpecialLen;
  1202.           if((buf = (STRPTR) AllocMem(buflen, MEMF_ANY|MEMF_CLEAR)))
  1203.       {
  1204.             if(!XpkPassRequestTags(XPK_PasswordBuf, buf,
  1205.             XPK_PassBufSize, buflen, TAG_DONE))
  1206.           xbi->xfdbi_Special = buf;
  1207.       }
  1208.         }
  1209.         else if(xbi->xfdbi_PackerFlags & XFDPFF_KEY16)
  1210.         {
  1211.       if(!XpkPassRequestTags(XPK_Key16BitPtr, &buflen, TAG_DONE))
  1212.         xbi->xfdbi_Special = &buflen;
  1213.         }
  1214.         else if(xbi->xfdbi_PackerFlags & XFDPFF_KEY32)
  1215.         {
  1216.       if(!XpkPassRequestTags(XPK_Key32BitPtr, &buflen, TAG_DONE))
  1217.             xbi->xfdbi_Special = &buflen;
  1218.         }
  1219.     CloseLibrary(xpkbase);
  1220.       }
  1221.       if(fd->fd_Flags & CHECKXFLAG_NODECRUNCH)
  1222.         ret = DoFileStrip(fd, buffer, buflength);
  1223.       else if(xfdDecrunchBuffer(xbi))
  1224.       {
  1225.         if((xbi->xfdbi_PackerFlags & XFDPFF_ADDR) &&
  1226.         !(fd->fd_Flags & CHECKXFLAG_ADDRESS))
  1227.         {
  1228.       fd->fd_Flags |= CHECKXFLAG_ADDRESS;
  1229.           PrintCHKXErr(fd, SaveUncrFile(fd, buffer, buflength));
  1230.         }
  1231.     fd->fd_Flags |= CHECKXFLAG_CRUNCHED;
  1232.         FreeCrunchMemList(fd, buffer);
  1233.         ++fd->fd_RecurseDepth;
  1234.         if(!(ret = AddCrunchMemList(fd, xbi->xfdbi_TargetBuffer,
  1235.         xbi->xfdbi_TargetBufLen)))
  1236.         {
  1237.           PrintCHKXErr(fd, DoGetVirus(fd, xbi->xfdbi_TargetBuffer,
  1238.             xbi->xfdbi_TargetBufSaveLen));
  1239.         }
  1240.         else
  1241.           FreeMem(xbi->xfdbi_TargetBuffer, xbi->xfdbi_TargetBufLen);
  1242.         --fd->fd_RecurseDepth;
  1243.       }
  1244.       else
  1245.         ret = XFDERR_OFFSET + xbi->xfdbi_Error;
  1246.  
  1247.       if(buf)
  1248.         FreeMem(buf, buflen);
  1249.     }
  1250.     else
  1251.       ret = DoFileStrip(fd, buffer, buflength);
  1252.  
  1253.     xfdFreeObject(xbi);
  1254.   }
  1255.   return ret;
  1256. }
  1257.  
  1258. /* Tries to strip useless hunks in a file. When there are some, we remove
  1259.    them and call DoGetVirus to start the loop again. Else we finish.
  1260.    When either unlinking or uncrunching happend in before loops, we may
  1261.    save the file when there was SAVE option.
  1262. */
  1263. static LONG DoFileStrip(struct FileData *fd, APTR buffer, ULONG buflength)
  1264. {
  1265.   LONG ret = 0;
  1266.   ULONG reslength = buflength;
  1267.  
  1268.   if(*((ULONG *) buffer) == 0x000003F3 && !(fd->fd_Flags &
  1269.   CHECKXFLAG_NOSTRIP))
  1270.     xfdStripHunks(buffer, buflength, &reslength,
  1271.     XFDSHF_NAME|XFDSHF_SYMBOL|XFDSHF_DEBUG);
  1272.     /* errors are not interpreted */
  1273.  
  1274.   if(buflength > reslength)
  1275.   {
  1276.     fd->fd_Flags |= CHECKXFLAG_STRIPPED;
  1277.     PrintCHKXTxt(fd, "%ld bytes stripped", buflength-reslength);
  1278.     ret = DoGetVirus(fd, buffer, reslength);
  1279.   }
  1280.   else if(!(fd->fd_Flags & CHECKXFLAG_ADDRESS))
  1281.     ret = SaveUncrFile(fd, buffer, buflength);
  1282.  
  1283.   FreeCrunchMemList(fd, buffer);
  1284.  
  1285.   return ret;
  1286. }
  1287.  
  1288. /* Print file name */
  1289. static void PrintCHKXFile(struct FileData *fd)
  1290. {
  1291.   STRPTR name = fd->fd_Name;
  1292.   UBYTE i;
  1293.  
  1294.   if(fd->fd_Flags & CHECKXFLAG_DEBUG)
  1295.   {
  1296.     for(i = 0; i < fd->fd_ArchiveDepth; ++i)
  1297.       KPutC('*');
  1298.     KPrintf("%s\n", name);
  1299.   }
  1300.  
  1301.   if(fd->fd_LogFileFH)
  1302.   {
  1303.     for(i = 0; i < fd->fd_ArchiveDepth; ++i)
  1304.       FPutC(fd->fd_LogFileFH, '*');
  1305.     FPrintf(fd->fd_LogFileFH, "%s\n", name);
  1306.   }
  1307.   if(!(fd->fd_Flags & CHECKXFLAG_QUIET))
  1308.   {
  1309.     for(i = 0; i < fd->fd_ArchiveDepth; ++i)
  1310.       FPutC(Output(), '*');
  1311.     Printf("%s\n", name);
  1312.   }
  1313.   fd->fd_Flags |= CHECKXFLAG_NAMEPRINTED;
  1314. }
  1315.  
  1316. static void PrintCHKXErr(struct FileData *fd, LONG err)
  1317. {
  1318.   if(err)
  1319.   {
  1320.     STRPTR txt = 0, txt2;
  1321.  
  1322.     if(err > XFDERR_OFFSET)
  1323.     {
  1324.       ++fd->fd_XFDErrors;
  1325.       txt2 = "XFD-Error %ld: %s";
  1326.       err -= XFDERR_OFFSET;
  1327.       txt = xfdGetErrorText(err);
  1328.     }
  1329.     else if(err > XADERR_OFFSET)
  1330.     {
  1331.       ++fd->fd_XADErrors;
  1332.       txt2 = "XAD-Error %ld: %s";
  1333.       err -= XADERR_OFFSET;
  1334.       txt = xadGetErrorText(err);
  1335.     }
  1336.     else if(err > CHXWARN_OFFSET)
  1337.     {
  1338.       txt2 = "CheckX-Warning %ld: %s";
  1339.       switch(err)
  1340.       {
  1341.       case CHKXWARN_NOVIRUS:    txt = "Virus-Checking disabled!";                        break;
  1342.       case CHKXWARN_XVSSELFTEST:txt = "The xvs.library is modified, maybe the system is virus infected!";    break;
  1343.       case CHKXWARN_MEMVIRUS:    txt = "Your system memory was virus infected!";                    break;
  1344.       }
  1345.       err -= CHXWARN_OFFSET;
  1346.     }
  1347.     else
  1348.     {
  1349.       ++fd->fd_CHKXErrors;
  1350.       txt2 = "CheckX-Error %ld: %s";
  1351.       switch(err)
  1352.       {
  1353.       case CHKXERR_NOMEMORY:    txt = "not enough memory";            break;
  1354.       case CHKXERR_EXAMINEERR:    txt = "examining failed";            break;
  1355.       case CHKXERR_OPENERR:    txt = "opening file failed";            break;
  1356.       case CHKXERR_READ:    txt = "reading failed";                break;
  1357.       case CHKXERR_SCANERR:    txt = "directory scan failed";            break;
  1358.       case CHKXERR_BREAK:    txt = "user break";                break;
  1359.       case CHKXERR_OPENDIR:    txt = "opening directory failed";        break;
  1360.       case CHKXERR_RESOURCE:    txt = "needed resource not available";        break;
  1361.       case CHKXERR_NOBOOTVIRUS:    txt = "could not check for bootblock virus";    break;
  1362.       case CHKXERR_WRITE:    txt = "writing failed";                break;
  1363.       case CHKXERR_NOVIRUS:    txt = "could not check for virus";        break;
  1364.       case CHKXERR_EMPTY:    txt = "file is empty";                break;
  1365.       }
  1366.     }
  1367.  
  1368.     PrintCHKXTxt(fd, txt2, err, txt);
  1369.   }
  1370. }
  1371.  
  1372. /* Print type text */
  1373. static void PrintCHKXTxt(struct FileData *fd, STRPTR txt, ...)
  1374. {
  1375.   UBYTE i;
  1376.  
  1377.   if(!(fd->fd_Flags & CHECKXFLAG_NAMEPRINTED))
  1378.     PrintCHKXFile(fd);
  1379.  
  1380.   if(fd->fd_Flags & CHECKXFLAG_DEBUG)
  1381.   {
  1382.     for(i = 0; i < fd->fd_RecurseDepth; ++i)
  1383.       KPutC(' ');
  1384.     RawDoFmt(txt, &txt+1, (void (*)()) KPutC, 0);
  1385.     KPutC('\n');
  1386.   }
  1387.  
  1388.   if(fd->fd_LogFileFH)
  1389.   {
  1390.     for(i = 0; i < fd->fd_RecurseDepth; ++i)
  1391.       FPutC(fd->fd_LogFileFH, ' ');
  1392.     VFPrintf(fd->fd_LogFileFH, txt, &txt+1);
  1393.     FPutC(fd->fd_LogFileFH, '\n');
  1394.     Flush(fd->fd_LogFileFH);
  1395.   }
  1396.  
  1397.   if(!(fd->fd_Flags & CHECKXFLAG_QUIET))
  1398.   {
  1399.     for(i = 0; i < fd->fd_RecurseDepth; ++i)
  1400.       FPutC(Output(), ' ');
  1401.     VPrintf(txt, &txt+1);
  1402.     FPutC(Output(), '\n');
  1403.   }
  1404. }
  1405.  
  1406. /* Add memory to the memory list. */
  1407. static LONG AddCrunchMemList(struct FileData *fd, APTR reg, ULONG size)
  1408. {
  1409.   struct CrunchMemList *ml;
  1410.  
  1411.   if((ml = (struct CrunchMemList *) AllocMem(sizeof(struct CrunchMemList),
  1412.   MEMF_ANY)))
  1413.   {
  1414.     ml->cml_Next = fd->fd_MemList;
  1415.     ml->cml_MemoryRegion = reg;
  1416.     ml->cml_MemorySize = size;
  1417.     fd->fd_MemList = ml;
  1418.     return 0;
  1419.   }
  1420.  
  1421.   return CHKXERR_NOMEMORY;
  1422. }
  1423.  
  1424. /* Free memory from the memory list. */
  1425. static void FreeCrunchMemList(struct FileData *fd, APTR reg)
  1426. {
  1427.   struct CrunchMemList mc, *ml = &mc;
  1428.  
  1429.   if(fd->fd_Flags & CHECKXFLAG_NOFREEMEM)
  1430.     return;
  1431.  
  1432.   for(mc.cml_Next = fd->fd_MemList; ml; ml = ml->cml_Next)
  1433.   {
  1434.     if(ml->cml_Next->cml_MemoryRegion == reg)
  1435.     {
  1436.       struct CrunchMemList *m;
  1437.       m = ml->cml_Next;
  1438.       ml->cml_Next = m->cml_Next;
  1439.       FreeMem(m->cml_MemoryRegion, m->cml_MemorySize);
  1440.       FreeMem(m, sizeof(struct CrunchMemList));
  1441.     }
  1442.   }
  1443.  
  1444.   fd->fd_MemList = mc.cml_Next;
  1445. }
  1446.  
  1447. /* Save file, when SAVE option and file was crunched or linked. */
  1448. static LONG SaveUncrFile(struct FileData *fd, APTR buf, ULONG size)
  1449. {
  1450.   BPTR filefh, cd;
  1451.   LONG ret = 0, i = 0;
  1452.   UBYTE name[50];
  1453.   UBYTE nbuf[256];
  1454.  
  1455.   if(!(fd->fd_SaveDirL && (fd->fd_Flags & CHKXSAVEFLAGS)))
  1456.     return 0;
  1457.  
  1458.   sprintf(name, (fd->fd_LinkNum ? "%s.%ld" : "%s"), FilePart(fd->fd_Name),
  1459.   fd->fd_LinkNum++);
  1460.  
  1461.   cd = CurrentDir(fd->fd_SaveDirL);
  1462.  
  1463.   while(!ret && name[i])
  1464.   {
  1465.     for(;name[i] && name[i] != '/'; ++i)
  1466.       nbuf[i] = name[i];
  1467.     if(name[i] == '/')
  1468.     {
  1469.       nbuf[i] = 0;
  1470.       if((filefh = Lock(nbuf, SHARED_LOCK)))
  1471.         UnLock(filefh);
  1472.       else
  1473.       {
  1474.         if((filefh = CreateDir(nbuf)))
  1475.           UnLock(filefh);
  1476.         else
  1477.           ret = CHKXERR_OPENERR;
  1478.       }
  1479.       nbuf[i] = name[i];
  1480.       ++i;
  1481.     }
  1482.   }
  1483.  
  1484.   if(!ret)
  1485.   {
  1486.     if((filefh = Open(name, MODE_NEWFILE)))
  1487.     {
  1488.       if(Write(filefh, buf, size) != size)
  1489.         ret = CHKXERR_WRITE;
  1490.       Close(filefh);
  1491.     }
  1492.     else
  1493.       ret = CHKXERR_OPENERR;
  1494.   }
  1495.  
  1496.   CurrentDir(cd);
  1497.  
  1498.   return ret;
  1499. }
  1500.  
  1501.